<?php
/**
* @Copyright Copyright (C) 2010 dhtmlx.com
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/

/**
 This file is part of dhtmlxScheduler for Joomla.

    dhtmlxScheduler for Joomla is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    dhtmlxScheduler for Joomla is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with dhtmlxScheduler for Joomla.  If not, see <http://www.gnu.org/licenses/>.
**/

defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');

class SchedulerController extends JController
{

	function display()
	{
		parent::display();
	}

	function loadxml() {
		require_once(JPATH_ROOT.DS.'components'.DS.'com_scheduler'.DS.'codebase'.DS.'dhtmlxSchedulerConfigurator.php');
		$table = 'scheduler_options';
		$tableEvents = 'events_rec';
		$fieldName = 'name';
		$fieldValue = 'value';
		$userIdField = 'id';
		$userLoginField = 'username';
		$tableUsers = 'users';

		$user =& JFactory::getUser();
		$usertype = $this->getUser();
		$userid = $user->id;

		$cfg = new JConfig;
		$prefix = $cfg->dbprefix;
		$res = mysql_connect($cfg->host, $cfg->user, $cfg->password);
		if (!$res) {
			die('Incorrect db configuration');
		}
		mysql_select_db($cfg->db, $res);

		$cfg = new SchedulerConfig('scheduler_config_xml', $res, $table, $fieldName, $fieldValue, $tableEvents, $userIdField, $userLoginField, $tableUsers, $prefix, $userid, true);
		if (isset($_GET['config_xml'])) {
			header('Content-type: text/xml');
			echo $cfg->getXML();
			die();
		} else {
			if (isset($_GET['grid_events'])) {
				$cfg->getEventsRecGrid(true);
			} else {
				if (isset($_GET['scheduler_events'])) {
					$cfg->getEventsRec($usertype, true);
					return "";
				}
			}
		}
	}


	function getUser() {
		$user =& JFactory::getUser();
		switch($user->usertype) {
			case '':
				$usertype = 'guest';
				break;
			case 'Super Administrator':
				$usertype = 'superadministrator';
				break;
			default:
				$usertype = strtolower($user->usertype);
			}
		return $usertype;
	}

}